(function($, owner) {
	/**
	 * 用户登录
	 **/
	owner.login = function(loginInfo) {

		loginInfo = loginInfo || {};
		loginInfo.account = loginInfo.account || '';
		loginInfo.password = loginInfo.password || '';
		if(loginInfo.account.length < 5) {
			plus.nativeUI.toast('账号最短为 5 个字符');
			return;
		}
		if(loginInfo.password.length < 6) {
			plus.nativeUI.toast('密码最短为 6 个字符');
			return;
		}
		var wt = plus.nativeUI.showWaiting("正在登录...");
		var res = userService.user_login(loginInfo.account, loginInfo.password); //用户登录
		console.log(JSON.stringify(res));
		if(res.resultCode == REQUEST_SUCCESS_CDOE) {
			wt.close();
			owner.createState(res);
			var list = plus.webview.currentWebview().opener();
			//触发列表界面的自定义事件（refresh）,从而进行数据刷新  
			mui.fire(list, 'refresh');
			mui.currentWebview.close();
		} else {
			wt.close();
			plus.nativeUI.toast(res.msg);
			return;
		}
	};

	owner.createState = function(res) {
		var state = owner.getState();
		
		state.loginToken = res.data.login_token;
		state.userNickeName = res.data.user_nick_name;
		state.userFace = USER_FACE_PATH + '/' + res.data.user_face;
		console.log(JSON.stringify(state));
		owner.setState(state);
		
	};

	/**
	 * 新用户注册
	 **/
	owner.reg = function(regInfo, callback) {
		callback = callback || $.noop;
		regInfo = regInfo || {};
		regInfo.account = regInfo.account || '';
		regInfo.password = regInfo.password || '';
		if(regInfo.account.length < 5) {
			return callback('用户名最短需要 5 个字符');
		}
		if(regInfo.password.length < 6) {
			return callback('密码最短需要 6 个字符');
		}
		if(!checkEmail(regInfo.email)) {
			return callback('邮箱地址不合法');
		}
		var res = userService.user_register(regInfo.account, regInfo.password, regInfo.email);
		if(res.resultCode === REQUEST_SUCCESS_CDOE) {
			return callback();
		} else {
			return callback(res.msg);
		}

	};

	/**
	 * 获取当前状态
	 **/
	owner.getState = function() {
		var stateText = localStorage.getItem('$state') || "{}";
		return JSON.parse(stateText);
		
	};

	/**
	 * 设置当前状态
	 **/
	owner.setState = function(state) {
		state = state || {};
		localStorage.setItem('$state', JSON.stringify(state));
	};

	owner.removeState = function() {
		localStorage.removeItem('$state');
	};

	var checkEmail = function(email) {
		email = email || '';
		return(email.length > 3 && email.indexOf('@') > -1);
	};

	/**
	 * 发送邮件
	 **/
	owner.sendEmail = function(email, sendMailBox, callback) {
		callback = callback || $.noop;
		if(!checkEmail(email)) {
			return callback('邮箱地址不合法');
		}

		var res = userService.send_email(email);
		if(res.resultCode === REQUEST_SUCCESS_CDOE) {
			settime(sendMailBox); //调用计时函数
			return callback(null, '随机码已发送邮箱');
		} else {
			return callback(res.msg);
		}

	};

	/**
	 * 重置密码
	 */
	owner.resetPassword = function(password, rePassword, email, vcode, callback) {
		callback = callback || $.noop;
		console.log(password);
		console.log(password === '');
		if(password === '') {
			return callback("请填写新密码");
		}
		if(rePassword === '') {
			return callback("请确认密码");
		}
		if(rePassword != password) {
			return callback("两次密码不一致");
		}
		var res = userService.reset_password(email, vcode, password);
		if(res.resultCode === REQUEST_SUCCESS_CDOE) {
			return callback(null, res.msg);
		} else {
			return callback(res.msg);
		}
	}

	/**
	 * 获取应用本地配置
	 **/
	owner.setSettings = function(settings) {
		settings = settings || {};
		localStorage.setItem('$settings', JSON.stringify(settings));
	}

	/**
	 * 设置应用本地配置
	 **/
	owner.getSettings = function() {
		var settingsText = localStorage.getItem('$settings') || "{}";
		return JSON.parse(settingsText);
	}
	/**
	 * 获取本地是否安装客户端
	 **/
	owner.isInstalled = function(id) {
		if(id === 'qihoo' && mui.os.plus) {
			return true;
		}
		if(mui.os.android) {
			var main = plus.android.runtimeMainActivity();
			var packageManager = main.getPackageManager();
			var PackageManager = plus.android.importClass(packageManager)
			var packageName = {
				"qq": "com.tencent.mobileqq",
				"weixin": "com.tencent.mm",
				"sinaweibo": "com.sina.weibo"
			}
			try {
				return packageManager.getPackageInfo(packageName[id], PackageManager.GET_ACTIVITIES);
			} catch(e) {}
		} else {
			switch(id) {
				case "qq":
					var TencentOAuth = plus.ios.import("TencentOAuth");
					return TencentOAuth.iphoneQQInstalled();
				case "weixin":
					var WXApi = plus.ios.import("WXApi");
					return WXApi.isWXAppInstalled()
				case "sinaweibo":
					var SinaAPI = plus.ios.import("WeiboSDK");
					return SinaAPI.isWeiboAppInstalled()
				default:
					break;
			}
		}
	}
}(mui, window.app = {}));